﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Magic.Core
{
    public static class EnumUtil
    {
        public static string GetEnumDescription<T>( T value) where T : Enum
        {
            Type enumType = typeof(T);
            var name = Enum.GetName(enumType, value);
            if (name == null)
                return string.Empty;
            object[] objs = enumType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (objs == null || objs.Length == 0)
                return string.Empty;
            DescriptionAttribute attr = objs[0] as DescriptionAttribute;
            return attr.Description;
        }

        public static Dictionary<string, int> ForEachEnum<T>() where T : Enum {
            Dictionary<string, int> enumDic = new Dictionary<string, int>();
            Type t = typeof(T);
            Array arrays = Enum.GetValues(t);
            for (int i = 0; i < arrays.LongLength; i++)
            {
                T e = (T)arrays.GetValue(i);
                FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
                object[] attribArray = fieldInfo.GetCustomAttributes(false);
                DescriptionAttribute attr = (DescriptionAttribute)attribArray[0];
                enumDic.Add(attr.Description, e.GetHashCode());
            }
            return enumDic;
        }
    }
}
